//
//  WBShareManager.m
//  WeatherBar
//
//  Created by Frank on 2024/6/28.
//

#import "WBShareManager.h"

@interface WBShareManager ()

@end

@implementation WBShareManager

static WBUploadManager *var_instance;

+ (instancetype)wb_shareInstance {
    if (var_instance==nil) {
        var_instance = [[super allocWithZone:NULL] init];
    }
    return var_instance;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [WBShareManager wb_shareInstance];
}

- (id)copyWithZone:(struct _NSZone *)zone {
    return [WBShareManager wb_shareInstance];
}
+ (void)wb_destroyInstance {
    var_instance = nil;
}


//检测用户手机是否有安装Facebook应用
- (BOOL)wb_checkInstall {
    NSURL *facebookURL = [NSURL URLWithString:@"fb://"];
    if ([[UIApplication sharedApplication] canOpenURL:facebookURL]) {
        return YES;
    } else {
        return NO;
    }
}

+ (void)wb_shareWithType:(NSString *)type contentUrl:(NSString *)contentUrl {
    [self wb_shareWithType:type contentUrl:contentUrl imgUrl:@"" urlDescription:@"" urlTitle:@""];
}

+ (void)wb_shareWithType:(NSString *)type contentUrl:(NSString *)contentUrl imgUrl:(NSString *)imgUrl urlDescription:(NSString *)urlDescription urlTitle:(NSString *)urlTitle {
    
    WBShareManager *var_instance = [WBShareManager wb_shareInstance];
    
    if ([type isEqualToString:@"facebook"]) {
        [var_instance wb_shareFbWithUrl:contentUrl description:urlDescription];
    }
    else if ([type isEqualToString:@"twitter"]) {
        [var_instance wb_shareTwitterWithUrl:contentUrl description:urlDescription];
    }
    else if ([type isEqualToString:@"whatsapp"]) {
        [var_instance wb_shareWhatsAppWithUrl:contentUrl description:urlDescription];
    }
    else if ([type isEqualToString:@"instagram"]) {
        [var_instance wb_shareInsWithUrl:contentUrl description:urlDescription];
    }
}

//fb分享
- (void)wb_shareFbWithUrl:(NSString *)url description:(NSString *)description {
    NSLog(@"fbtest share1: %@", url);
//    NSLog(@"fbtest share2: %@", description);
    
    FBSDKShareLinkContent*linkContent = [[FBSDKShareLinkContent alloc]init];
    linkContent.contentURL= [NSURL URLWithString:url];
    
    FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc] initWithViewController:[UIViewController currentViewController] content:linkContent delegate:self];
//    shareDialog.mode = FBSDKShareDialogModeNative;
//    if (![shareDialog canShow]) {
//        shareDialog.mode = FBSDKShareDialogModeWeb;
//    }
//    shareDialog.shareContent = linkContent;
    [shareDialog show];
//    NSLog(@"fbtest: shareUrl ShareLinkContent");
}
//Twitter分享
- (void)wb_shareTwitterWithUrl:(NSString *)url description:(NSString *)description {
    NSLog(@"fbtest share1: %@", url);
//    NSLog(@"fbtest share2: %@", description);
    NSString *var_string = [NSString stringWithFormat:@"%@",url];
    NSURL *var_url = [NSURL URLWithString:var_string];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]
                                            initWithActivityItems:@[description, var_url]
                                            applicationActivities:nil];
//    activityVC.excludedActivityTypes = [self excludetypes];
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable   activityType,
                                              BOOL completed,
                                              NSArray * _Nullable returnedItems,
                                              NSError * _Nullable activityError) {
        NSLog(@"activityType: %@,\n completed: %d,\n returnedItems:%@,\n activityError:%@",activityType,completed,returnedItems,activityError);
    };
    [[UIViewController currentViewController] presentViewController:activityVC animated:YES completion:nil];
}
//whatsApp分享
- (void)wb_shareWhatsAppWithUrl:(NSString *)url description:(NSString *)description {
    NSLog(@"fbtest share1: %@", url);
//    NSLog(@"fbtest share2: %@", description);
    NSString *var_string = [NSString stringWithFormat:@"whatsapp://send?text=%@",url];
    NSURL *var_url = [NSURL URLWithString:var_string];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]
                                            initWithActivityItems:@[description, var_url]
                                            applicationActivities:nil];
//    activityVC.excludedActivityTypes = [self excludetypes];
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable   activityType,
                                              BOOL completed,
                                              NSArray * _Nullable returnedItems,
                                              NSError * _Nullable activityError) {
        NSLog(@"activityType: %@,\n completed: %d,\n returnedItems:%@,\n activityError:%@",activityType,completed,returnedItems,activityError);
    };
    [[UIViewController currentViewController] presentViewController:activityVC animated:YES completion:nil];
}

//ins分享
- (void)wb_shareInsWithUrl:(NSString *)url description:(NSString *)description {
    NSLog(@"fbtest share1: %@", url);
//    NSLog(@"fbtest share2: %@", description);
    NSString *var_string = [NSString stringWithFormat:@"instagram://send?text=%@",url];
    NSURL *var_url = [NSURL URLWithString:var_string];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]
                                            initWithActivityItems:@[description, var_url]
                                            applicationActivities:nil];
//    activityVC.excludedActivityTypes = [self excludetypes];
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable   activityType,
                                              BOOL completed,
                                              NSArray * _Nullable returnedItems,
                                              NSError * _Nullable activityError) {
        NSLog(@"activityType: %@,\n completed: %d,\n returnedItems:%@,\n activityError:%@",activityType,completed,returnedItems,activityError);
    };
    [[UIViewController currentViewController] presentViewController:activityVC animated:YES completion:nil];
}

#pragma mark - FBSDKSharingDelegate

- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary<NSString *,id> *)results {
//    if ([sharer isKindOfClass:[FBSDKShareDialog class]]) {
//        NSLog(@"fbtest: share onSuccess");
//        // Dispatch share success event
//    } else if ([sharer isKindOfClass:[FBSDKMessageDialog class]]) {
//        NSLog(@"fbtest: invite onSuccess");
//        // Dispatch invite success event
//    }
    NSLog(@"fbtest: share onSuccess");
}
 
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {
    //    if ([sharer isKindOfClass:[FBSDKShareDialog class]]) {
    //        NSLog(@"fbtest: share onError %@", error);
    //        // Dispatch share error event
    //    } else if ([sharer isKindOfClass:[FBSDKMessageDialog class]]) {
    //        NSLog(@"fbtest: invite onError %@", error);
    //        // Dispatch invite error event
    //    }
    NSLog(@"fbtest: share onError %@", error);
}

- (void)sharerDidCancel:(id<FBSDKSharing>)sharer {
    //    if ([sharer isKindOfClass:[FBSDKShareDialog class]]) {
    //        NSLog(@"fbtest: share onCancel");
    //        // Dispatch share cancel event
    //    } else if ([sharer isKindOfClass:[FBSDKMessageDialog class]]) {
    //        NSLog(@"fbtest: invite onCancel");
    //        // Dispatch invite cancel event
    //    }
    NSLog(@"fbtest: share onCancel");
}

@end
